<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/3/30
 * Time: 15:21
 */

namespace common\models\datas;

use common\components\bases\BaseModel;
use common\components\extenders\MenuManage;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

class Customergrade extends BaseModel
{
    public static function tableName(){
        return '{{%customer_grade}}';
    }

    public function rules(){
        return [
            [['name'],'unique'],
            [['name','need_coin'], 'required'],
            [['created_at', 'updated_at','need_coin'], 'integer'],
        ];
    }

    public function fields(){
        $fields = parent::fields();
        $fields['created_at'] = function(){
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['updated_at'] = function(){
            return \Yii::$app->formatter->asDatetime($this->updated_at);
        };
        $fields['is_delete'] = function () {
            switch ($this->is_delete) {
                case 0:
                    return \Yii::t('common', '有效');
                case 1:
                    return \Yii::t('common', '删除');
            }
        };
        return $fields;
    }

    public function extraFields(){
        return [
            'backend_operate' => function(){
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->isDisable(
                    ['delete' => function ($data) {
                        if ($data['id'] == 1) {
                            return '<button class="yx-btn-disabled" type="button">删除</butt>';
                        } else {
                            return '';
                        }
                    },
                    ]
                )->create();
            }
        ];
    }

    public function attributeLabels(){
        return [
            'id' => \Yii::t('common','自动编号'),
            'name' => \Yii::t('common','会员等级名'),
            'need_coin' => \Yii::t('common','所需积分'),//暂时没用，用于如果后期会员根据消费金额自动升级
            'created_at' => \Yii::t('common','创建时间'),
            'updated_at' => \Yii::t('common','修改时间'),
            'is_delete'=>\Yii::t('common','是否删除'),
        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
        ]);
    }
}